let login_id ;
$(document).ready(function(){
    var login = isLogin();
    if (login == undefined) {
        alert("去登陆")
        location.href = "login.html";
    }
    $.ajax({
        type: "get",
        url: "http://localhost:3301/user?phone=" + login,
        async: false,
        success: function (e) {
            login_id = e[0].id;
        }
    })
    show_address();

    $(".address-item .delete").click(function(){
        if(confirm("确定要删除吗？")){
            let aid = $(this).attr("data-id");
            delete_address(aid);
            show_address(); 
        }
    })
    $(".addbtn .btn-info").click(function(){
        $("#myModalLabel").html("添加收货地址");
        show_box();
    })
    $(".modal-footer .btn-default").click(function(){
        close_box();
    })
    $(".sui-modal-backdrop").click(function(){
        close_box();
    })
    $(".sui-close").click(function(){
        close_box();
    })
    $(".pull-right .edit").click(function(){
        $("#myModalLabel").html("编辑收货地址");
        show_box();
        let addr = get_address($(this).attr("data-id"));
        $(".form-horizontal [name=id]").val(addr.id);
        $(".form-horizontal [name=consignee]").val(addr.consignee);
        $(".form-horizontal [name=province]").val(addr.province);
        cityShow();
        $(".form-horizontal [name=city]").val(addr.city);
        district();
        $(".form-horizontal [name=district]").val(addr.district);
        let adr = $(".form-horizontal [name=address]").val(addr.address);
        let phone = $(".form-horizontal [name=phone]").val(addr.phone);
        $(".form-horizontal [name=is_default]").prop("checked", addr.is_default == "0" ? false : true);
        return false;
    })
    $(".smt .btn-warning").click(function(){
        let aid = $(this).attr("data-id");
        put_address_is_selected(aid);
        show_address();
    })
    $("#sub_address").click(function(){
        show_address();
    })
})

//展示登陆用户的收货地址信息
function show_address(){
    let addresses = $(".addresses");
    let addr = get_address_all(login_id);
    addr.forEach((e,i)=>{
        let item = $(`<div class="address-item">
        <div class="smt">${e.consignee}&nbsp;&nbsp;
            <a id="J_set" href="javascript:void(0)" data-toggle="tooltip" data-trigger="click" title="" data-original-title="更改收货人姓名&nbsp;&nbsp;<button class='sui-btn btn-small btn-danger'>保存</button><br><input type=&quot;text&quot;>">设置 </a>&nbsp;&nbsp;
            <i class="sui-btn btn-warning" data-id="${e.id}" style="${e.is_default=="0"?"":"background:#b1191a;"}">默认地址</i>
            <a href="#" class="pull-right delete" data-id="${e.id}">删除</a>
        </div>
        <div class="smc">
            <div class="item">
                <span class="fl label">收货人：</span>
                <div class="detail">${e.consignee}</div>
                <div class="clearfix"></div>
            </div>
        </div>
        <div class="smc">
            <div class="item">
                <span class="fl label">所在地区：</span>
                <div class="detail">${e.area}</div>
                <div class="clearfix"></div>
            </div>
        </div>
        <div class="smc">
            <div class="item">
                <span class="fl label">地址：</span>
                <div class="detail">${e.area}${e.address}</div>
                <div class="clearfix"></div>
            </div>
        </div>
        <div class="smc">
            <div class="item">
                <span class="fl label">手机：</span>
                <div class="detail">${e.phone}</div>
                <div class="clearfix"></div>
            </div>  
        </div>
        <div class="pull-right operate">
            <a href="#" data-toggle="modal" data-target=".editinfo" data-keyboard="false">使用一键购</a>
            <a href="#" data-toggle="modal" data-target=".edit" data-keyboard="false" data-id="${e.id}" class="edit">编辑</a>
        </div>
        <div class="clearfix"></div>
    </div>`);
    addresses.append(item);
    })
}